﻿  
using SHKF.B2C.BLL.Center;
using SHKF.B2C.MeiTuanShanGou.Model.Entity.Medicine;
 
using SqlSugar; 
using System.Collections.Generic;
 
 

namespace SHKF.B2C.MeiTuanShanGou.BLL
{
    /// <summary>
    /// 产品
    /// </summary>
    public class MedicineBLL : CenterBLL
    { 
        private readonly IdentityBLL productIDBLL;

        public MedicineBLL(SqlSugarClient db ,
            IdentityBLL productIDBLL):
            base(db)
        { 
            this.productIDBLL = productIDBLL;
        }

        public int Save(ProductInfoMtsgEntity product)
        {
            int iResult = 0;
            ProductInfoMtsgEntity medicineEntity = this.db.Queryable<ProductInfoMtsgEntity>()
                .Where(oo => oo.ShopSN == product.ShopSN
                                    && oo.Upc == product.Upc)
                .First();
            if (medicineEntity != null)
            {
                product.SN = medicineEntity.SN;
                iResult = this.db
                    .Updateable<ProductInfoMtsgEntity>(product) 
                    .ExecuteCommand();
            }
            else
            { 
                iResult = this.db
                    .Insertable<ProductInfoMtsgEntity>(product)
                    .ExecuteCommand();
            }

            return iResult;
        }

        public string GetID(long ProductSN, long ShopSN)
        {
            string id = this.db.Queryable<ProductInfoMtsgEntity>()  
                .Where(oo => oo.ShopSN == ShopSN
                                    && oo.ProductSN == ProductSN)
                .Select(it => it.ID)
                .First();
            return id;
        }

        public ProductInfoMtsgEntity Get(long ProductSN, long ShopSN)
        {
            ProductInfoMtsgEntity info = this.db.Queryable<ProductInfoMtsgEntity>()
               .Where(oo => oo.ShopSN == ShopSN
                                   && oo.ProductSN == ProductSN)
               .First();
            return info;
        }

        public long GetProductSN(string upc, long app_poi_code)
        {
            //获取erp的产品SN,用于回调
            long productSN = this.db
                .Queryable<ProductInfoMtsgEntity>()
                .Where(ww => ww.Upc == upc && ww.app_poi_code == app_poi_code)
                .Select(a => a.ProductSN)
                .First();
            return productSN;
        }

        public MedicineCatEntity GetDefaultMedicineCat(long app_poi_code)
        {
            MedicineCatEntity medicineCat = this.db.Queryable<MedicineCatEntity>()
                .Where(ww => ww.app_poi_code == app_poi_code)
                .First();

            return medicineCat;
        }

        /// <summary>
        /// 获取当前门店的产品
        /// </summary>
        /// <param name="app_poi_code">门店SN</param>
        /// <returns></returns>
        public List<ProductInfoMtsgEntity> Query(long ShopSN)
        {
            List<ProductInfoMtsgEntity> list = this.db
                .Queryable<ProductInfoMtsgEntity>()
                .Where(oo => oo.ShopSN == ShopSN)
                .ToList();

            return list;
        }

        public int Delete(long ShopSN, long ProductSN)
        {
            var cmd = this.db
                .Deleteable<ProductInfoMtsgEntity>();

            if (ShopSN > 0)
            {
                cmd.Where(oo => oo.ShopSN == ShopSN);
            }

            if (ProductSN > 0)
            {
                cmd.Where(oo => oo.ProductSN == ProductSN);
            }

            int iResult = cmd.ExecuteCommand(); 
            return iResult;
        }
    }
}
